﻿using System;
using System.Collections.Generic;
using System.Linq;
using Web.Models;
using Model.Core.DataBase;
using Web.Models.AnalysisData;
using Web.Models.CoreData;


namespace Web.BML.CoreData
{
    /// <summary>
    /// 图库管理
    /// </summary>
    public class GalleryOp
    {
        private static GalleryOp instance;
        /// <summary>
        /// 构造函数
        /// </summary>
        private GalleryOp()
        {

        }
        /// <summary>
        /// 单例模式实例化
        /// </summary>
        /// <returns></returns>
        public static GalleryOp GetInstance()
        {
            if (instance == null)
            {
                instance = new GalleryOp();
            }
            return instance;
        }


  
        /// <summary>
        ///    查询单条配置信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public Gallery Query(String id)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<Gallery>().FirstOrDefault(m => m.Id == id);
            }
        }

        /// <summary>
        ///  查询用户配置信息-通过用户id
        /// </summary>
        /// <param name="companyId">用户id</param>
        /// <returns></returns>
        public List<Gallery> QueryByCompanyId(String companyId)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<Gallery>().Where(m => m.CompanyId.Equals(companyId)).ToList();
            }
        }

        /// <summary>
        /// 查询所有配置信息
        /// </summary>
        /// <returns></returns>
        public List<Gallery> QueryAll()
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<Gallery>().ToList();
            }
        }
        
        /// <summary>
        /// 修改配置信息
        /// </summary>
        /// <param name="model">数据</param>
        public bool ModifGallery(Gallery model)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                db.GetOperator<Gallery>().Modify(model);
                return db.SaveAllChange() == 1;
            }
        }

        /// <summary>
        /// 创建配置信息
        /// </summary>
        /// <param name="model">数据</param>
        public bool CreateGallery(Gallery model)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                db.GetOperator<Gallery>().Create(model);
                return db.SaveAllChange()==1;
            }
        }

        /// <summary>
        /// 删除配置信息
        /// </summary>
        /// <param name="id">数据id</param>
        public bool DeleteGallery(Gallery gallery)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                db.GetOperator<Gallery>().Destroy(gallery);
                return db.SaveAllChange() == 1;
            }
        }
    }


}

